'use strict';
//process.env.NODE_ENV="development";//"production";//
process.env.NODE_ENV="production";//
var User = require('./user.model');
var mongoose = require('mongoose');
var config = require('../../config/environment');
var lineReader = require('line-reader');
// console.log(config.mongo.uri, config.mongo.options);
// // Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options);
//////////////////////////////////////////
//插入管理员
//   User.create({
//     provider: 'local',
//     role: ['admin'],
//     code: 'admin',
//     name: 'Admin',
//     email: 'admin@admin.com',
//     password: 'admin'
//   }, function() {
//       console.log('finished populating users');
//     }
//   );


///////////////////////////////////////////
var file=process.argv[2];
var klass=process.argv[3];
if(!(file||klass)){
  console.log("输入学生数据文件和班级代码");
  console.log("例如：node server/api/user/import.js student.csv 201423");
  process.exit(1);
}
lineReader.eachLine(file, function(line, last,cb) {
  console.log(line);
  var student=line.split(',');
  console.log(student[0],student[1]);
  var name = student[0];
  var code = student[1];
  var user = new User();
  user.code = code;
  user.name = name;
  user.password="";
  user.klass = klass;
  user.role = ['student'];
  user.save(function(err,doc){
    if(err){
      console.log(err);
      cb(false);
    }else{
      cb();
    }
  })
}).then(function () {
  console.log("任务完成！");
  process.exit(1);
});
